package com.nearby.android;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.nearby.android.unitl.Common;
import com.nearby.android.unitl.Constant;
import com.nearby.android.unitl.Message;

public class ClockIn extends Activity {
	private Button clockin;
	private Button look_history_clockin;
	private Socket clientSocket;
	private PrintStream ps; //输出
	private BufferedReader br; //输入
	private Message msg;  //信息格式
	private String sendMsg; //发送信息内容
	private String recvMsg; //接受信息内容

	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.clockin);
		
	
		initWin();
		initBind();
	}
	public void initWin(){
		clockin = (Button) findViewById(R.id.clockin);
		look_history_clockin = (Button) findViewById(R.id.look_history_clockin);
	}
	public void initBind(){
		clockin.setOnClickListener(new MyClickListener());
		look_history_clockin.setOnClickListener(new MyClickListener());
	}
	
	class MyClickListener implements View.OnClickListener{
		@Override
		public void onClick(View v) {
			switch(v.getId()){
			case R.id.clockin:
				try {
					clientSocket = new Socket(Common.getServerSocketConfig(ClockIn.this).get("serverip"),
						Integer.valueOf(Common.getServerSocketConfig(ClockIn.this).get("serverport")));
					
					msg = new Message();
					msg.setCommand(Constant.SOCKET_CLOCKIN);
					msg.setSendTo(Constant.SOCKET_SERVER);
					msg.setFromTo(Common.getPhoneNumber(ClockIn.this) + Constant.SOCKET_MSG_SPLIT + Common.getPhoneMac(ClockIn.this));
					msg.setMsgBody(null);
					
					sendMsg = msg.Msg2String();
					//byte[] sendByte = sendMsg.getBytes("UTF-8");
					
					ps = new PrintStream(clientSocket.getOutputStream());
					ps.print(sendMsg);
					
					//接受服务端发送过来的信息
					br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
				    
					StringBuilder sb = new StringBuilder();
					while((recvMsg = br.readLine()) != null){
						sb.append(recvMsg);
					}
					//转换成Message对象
					Message newmsg = Message.Str2Message(sb.toString());
					if((newmsg.getSendTo() == msg.getFromTo()) && newmsg.getClass().equals(Constant.SOCKET_CLOCKIN_RETURN)){
						String msgbody = newmsg.getMsgBody();
						String[] bodys = msgbody.split(Constant.SOCKET_MSG_SPLIT);
						if(bodys[0].equals(Constant.SOCKET_CLOCKIN_SUCCESS)){
							Toast.makeText(ClockIn.this, "打卡成功", Toast.LENGTH_SHORT).show();
						}else if(bodys[0].equals(Constant.SOCKET_CLOCKIN_FAILURE)){
							Toast.makeText(ClockIn.this, "打卡失败:" + bodys[1], Toast.LENGTH_SHORT).show();
						}
					}
				} catch (NumberFormatException e) {
					e.printStackTrace();
				} catch (UnknownHostException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}finally{
					if(clientSocket != null){
						try {
							clientSocket.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
					if(ps != null){
						ps.close();
					}
					if(br != null){
						try {
							br.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
				
				break;
			case R.id.look_history_clockin:
				Intent look_history_clockin = new Intent(ClockIn.this, HistoryClockin.class);
				startActivity(look_history_clockin);
				break;
			}
		}
		
	}
}
